package com.canplay.screen.utils

import android.os.Parcelable
import com.tencent.mmkv.MMKV

//基于MMKV的缓存工具
object CacheUtils {

    private val kv = MMKV.defaultMMKV()!!

    fun encode(key: String, value: Boolean) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: Int) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: Long) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: Float) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: Double) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: String?) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: ByteArray) {
        kv.encode(key, value)
    }

    fun encode(key: String, value: Parcelable?) {
        kv.encode(key, value)
    }

    fun decodeBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return kv.decodeBool(key, defaultValue)
    }

    fun decodeInt(key: String, defaultValue: Int = 0): Int {
        return kv.decodeInt(key, defaultValue)
    }

    fun decodeLong(key: String, defaultValue: Long = 0L): Long {
        return kv.decodeLong(key, defaultValue)
    }

    fun decodeFloat(key: String, defaultValue: Float = 0f): Float {
        return kv.decodeFloat(key, defaultValue)
    }

    fun decodeDouble(key: String, defaultValue: Double = 0.0): Double {
        return kv.decodeDouble(key, defaultValue)
    }

    fun decodeString(key: String, defaultValue: String? = ""): String? {
        return kv.decodeString(key, defaultValue)
    }

    fun decodeByteArray(key: String, defaultValue: ByteArray = byteArrayOf()): ByteArray {
        return kv.decodeBytes(key, defaultValue)!!
    }

    fun <T : Parcelable> decodeParcelable(key: String, tClass: Class<T>): T? {
        return kv.decodeParcelable(key, tClass)
    }

    fun removeValueForKey(key:String){
        kv.removeValueForKey(key)
    }

    fun clearAll(){
        kv.clearAll()
    }


}